//
//  Project_Step4ViewController.m
//  CMVRFForIOS
//
//  Created by zhangnorman on 15/4/20.
//  Copyright (c) 2015年 hvacapp. All rights reserved.
//

#import "Project_Step4ViewController.h"
#import "Project_Step3ViewController.h"
#import "Project_Step45ViewController.h"
#import "Project_Step5ViewController.h"
#import "Project_Step6ViewController.h"
#import "BottomActionView.h"
#import "Project_TabViewController.h"
#import "RoomData.h"
#import "SystemConfigTableViewCell.h"
#import "EditorTableViewCell.h"
#import "MachineSelectViewController.h"
#import "ProjectManager.h"
#import "GuidData.h"
#import <MBProgressHUD.h>
#import "swjPostData.h"
#import "UITapView.h"
#import "SelectXLViewController.h"
#import "MainEquipSystemData.h"
#import "SwjInfoTableViewCell.h"
#import "MachineSelectTableViewCell.h"
#import <CustomIOSAlertView.h>
#import "ProductListViewController.h"
#import "Project_Step8ViewController.h"
#import "ProjectContentViewController.h"

#define CELL_TYPE_SYSTEM 0
#define CELL_TYPE_MACHINE 1
#define CELL_TYPE_ROOM 2

typedef enum{
    KKSELECTED = 1,
    KKSELECTED_OTHER,
    KKSELECTED_NOT
}KSelectStatus;

@interface Project_Step4ViewController (){
    double temp_coeff;
}

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) BottomActionView *footerView;
@property (nonatomic,strong) NSMutableArray *tempRooms;

@end

@implementation Project_Step4ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initData];
    [self setupViews];
    
}

/**
 *  初始化数据
 */
- (void)initData {
    self.tempRooms = [NSMutableArray new];
    self.tempRooms = [kProject.rooms mutableCopy];
}



- (void)loadNextPage {
    if (![Tools isEmpty:kProject.qrjhq_series]) {
        Project_Step45ViewController *view = [Project_Step45ViewController new];
        [self.navigationController pushViewController:view animated:YES];
    } else if (![Tools isEmpty:kProject.kqnrsj_series]) {
        Project_Step5ViewController *view = [Project_Step5ViewController new];
        [self.navigationController pushViewController:view animated:YES];
    } else if (![Tools isEmpty:kProject.water_cleaner_series]) {
        Project_Step6ViewController *view = [Project_Step6ViewController new];
        [self.navigationController pushViewController:view animated:YES];
    } else {
        //报告页
        Project_Step8ViewController *view = [Project_Step8ViewController new];
        view.userName = kProject.name;
        view.project_id = kProject.project_id;
        view.project_name = kProject.project_name;
        [self.navigationController pushViewController:view animated:YES];
    }
}

#pragma mark - 初始化视图
- (void)setupViews{
    self.title = @"空调地暖配置";
    
    //自定义返回按钮
    UIBarButtonItem *backBtn = [[UIBarButtonItem alloc] initWithTitle:@"项目" style:UIBarButtonItemStyleDone target:self action:@selector(onBackToProject)];
    self.navigationItem.leftBarButtonItem = backBtn;
    
    //添加按钮
    UIBarButtonItem *addBtn = [[UIBarButtonItem alloc] initWithTitle:@"添加" style:UIBarButtonItemStyleDone target:self action:@selector(onAddBtnClick)];
    self.navigationItem.rightBarButtonItem = addBtn;
    
    
    //底部的上一步，下一步按钮
    self.footerView = [[BottomActionView alloc]initWithFrame:CGRectMake(0, self.view.height - KAction_Bar_Height - kNavigation_Bar_Height - kStatus_Bar_Height, kScreen_Width, KAction_Bar_Height)];
    if (iPhone_X) {
        self.footerView.y -= 34;
    }
    __block Project_Step4ViewController *tmp = (Project_Step4ViewController*)self;
    [self.footerView addLeftBtnBlock:^(id obj) {
        //TODO
        [[ProjectManager manager]goLastPage];
        [tmp.navigationController popViewControllerAnimated:YES];
        
    }];
    [self.footerView addRightBtnBlock:^(id obj) {
        [[ProjectManager manager]goNextPage];
        [tmp loadNextPage];
//        Project_Step45ViewController *view = [Project_Step45ViewController new];
//        [tmp.navigationController pushViewController:view animated:YES];
    }];
    [self.footerView setLeftBtnTitle:@"上一步" andRightBtnTitle:@"下一步" andCenterTitle:[[ProjectManager manager]getCurrentStepInfo]];
    [self.view addSubview:self.footerView];
    
    //列表
    self.tableView = [[UITableView alloc] initWithFrame:[UIView frameWithOutNavBottom] style:UITableViewStylePlain];
    [self.tableView registerClass:[SwjInfoTableViewCell class] forCellReuseIdentifier:@"swj"];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"room"];
    [self.tableView registerClass:[EditorTableViewCell class] forCellReuseIdentifier:@"main_equip"];
    [self.tableView registerClass:[MachineSelectTableViewCell class] forCellReuseIdentifier:@"main_equip_cell"];
    self.tableView.backgroundView = [[UIView alloc]init];
    self.tableView.backgroundColor = CM_TABLE_BG_COLOR;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    //去掉多余的分割线
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:self.tableView];
    
    //下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    [self.tableView.mj_header beginRefreshing];
}

#pragma mark - 事件响应
/**
 *  直接跳转到项目页面
 */
-(void)onBackToProject {
    //返回到首页
    for (UIViewController *tmp in self.navigationController.viewControllers) {
        if ([tmp isKindOfClass:[Project_TabViewController class]]) {
            [(Project_TabViewController*)tmp refreshData];
            [self.navigationController popToViewController:tmp animated:YES];
        }
    }
}

- (void)loadData {
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:kProject.project_id forKey:@"project_id"];
    [HttpClient getWithUrl:HTTP_GET_PROJECT_MAIN_EQUIP_SYSTEMS token:[AccountManager manager].token paramDic:params resultClass:[ProjectData class] success:^(ProjectData *obj) {
        [self.tableView.mj_header endRefreshing];
        if (obj.main_equip_systems != nil) {
            kProject.main_equip_systems = [obj.main_equip_systems mutableCopy];
        } else {
            kProject.main_equip_systems = [NSMutableArray new];
        }
        if (obj.rooms != nil) {
            kProject.rooms = [obj.rooms mutableCopy];
        } else {
            kProject.rooms = [NSMutableArray new];
        }
        [self.tableView reloadData];
        if (kProject.coeff_input > 0) {
            [self editCoeff:0 couldCancel:NO];
            kProject.coeff_input = 0;
        }
    } failure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
    }];
}


//添加一套系统
-(void)onAddBtnClick {
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:kProject.project_id forKey:@"project_id"];
    [MBProgressHUD showMessage:@"数据提交中..." toView:self.view];
    [HttpClient getWithUrl:HTTP_GET_PROJECT_MAIN_EQUIP_SYSTEM_ADD token:[AccountManager manager].token paramDic:params resultClass:[MainEquipSystemData class] success:^(MainEquipSystemData *obj) {
        [MBProgressHUD hideHUDForView:self.view];
        [kProject.main_equip_systems addObject:obj];
        [self editCoeff:kProject.main_equip_systems.count - 1 couldCancel:NO];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view];
        [Tools showMessageCenter:@"添加失败！"];
    }];
}

#pragma mark - tableView显示设置数据
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return kProject.main_equip_systems.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    MainEquipSystemData *data = [kProject.main_equip_systems objectAtIndex:section];
    if (data.equips != nil) {
        return 2 + self.tempRooms.count + data.equips.count;
    } else {
        return 2 + self.tempRooms.count;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MainEquipSystemData *item =  [kProject.main_equip_systems objectAtIndex:indexPath.section];
    if (indexPath.row == 0) {
        //系统cell
        SwjInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"swj"];
        [cell setData:item];
//        [cell setSubContentClosed:item.isClosed];
        cell.coeffModifyClicked = ^(void) {
            [self editCoeff:indexPath.section couldCancel:YES];
        };
        //滑动按钮
        cell.rightUtilityButtons = [self rightButtons];
        cell.delegate = self;
        cell.tag = 1000 + indexPath.section;
        return cell;
    } else {
        NSInteger roomIndex = 0, equipIndex = 0;
        //先展示房间、再展示主机
        roomIndex = indexPath.row - 1;
        equipIndex = indexPath.row - self.tempRooms.count - 2;
        if (roomIndex < self.tempRooms.count) {
            //房间
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"room"];
            cell.textLabel.textColor = CM_TABLE_TITLE_COLOR;
            cell.textLabel.font = [UIFont systemFontOfSize:kListTitleFontSize];
            
            RoomData *roomData = [self.tempRooms objectAtIndex:roomIndex];
            cell.textLabel.x = cell.textLabel.x + 2*CELL_CONTENT_MARGIN;
            cell.textLabel.text = roomData.room_name;
            KSelectStatus status = [self checkRoomStatus:roomData inSWJ:item];
            if (status == KKSELECTED) {
                cell.accessoryType = UITableViewCellAccessoryCheckmark;
            }
            else if (status == KKSELECTED_OTHER) {
                cell.accessoryType = UITableViewCellAccessoryNone;
                cell.textLabel.textColor = BSBlackTextColor1;
            }
            else {
                cell.textLabel.textColor = CM_PLACEHOLDER_TEXT_COLOR;
                cell.accessoryType = UITableViewCellAccessoryNone;
            }
            return cell;
        } else if (roomIndex == self.tempRooms.count) {
            EditorTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"main_equip"];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            [cell setTitleStr:@"主机" andColor:BSBlackTextColor andHintStr:nil andInputViewIsHidden:YES andInfoStr:nil andUnit:nil];
            [cell setNavigationHint:@"手动选择"];
            return cell;
        } else {
            //主机设备
            MainEquipData *data = [item.equips objectAtIndex:equipIndex];
            MachineSelectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"main_equip_cell"];
            [cell setNumber:data.count andUnit:@"台"];
            [cell setTitle:data.equip_model];
            cell.SteperValueChangedBlock = ^(int number){
                if (number > 0) {
                    [self updateMainEquipCount:number andSection:indexPath.section andIndex:equipIndex];
                } else {
                    [self deleteMainEquip:indexPath.section andIndex:equipIndex];
                }
            };
            //滑动按钮
            cell.rightUtilityButtons = [self rightButtons];
            cell.delegate = self;
            cell.tag = 100*indexPath.section + equipIndex;
            return cell;
        }
    }
    
}

-(NSArray *)rightButtons {
    NSMutableArray *rightUtilityButtons = [NSMutableArray new];
    [rightUtilityButtons sw_addUtilityButtonWithColor:[UIColor redColor] title:@"删除"];
    return rightUtilityButtons;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 0) {
        //系统cell
        [self editCoeff:indexPath.section couldCancel:YES];
    } else {
        NSInteger roomIndex = 0, equipIndex = 0;
        roomIndex = indexPath.row - 1;
        equipIndex = indexPath.row - self.tempRooms.count - 2;
        if (roomIndex < self.tempRooms.count) {
            //房间
            RoomData *roomData = [self.tempRooms objectAtIndex:roomIndex];
            [self modifyMainEquipSystemRoom:roomData.project_room_id andSection:indexPath.section];
        } else if (roomIndex == self.tempRooms.count) {
            //选择主机
            MainEquipSystemData *item =  [kProject.main_equip_systems objectAtIndex:indexPath.section];
            ProductListViewController *vc = [ProductListViewController new];
            vc.product_type = @"main_equip";
            vc.project_id = kProject.project_id;
            vc.situation_type = 1;
            vc.project_id = kProject.project_id;
            vc.title = @"选择主机";
            vc.series = kProject.main_equip_series;
            if (item.equips.count > 0) {
                vc.selectedGuids = [NSMutableArray new];
                for (MainEquipData *data in item.equips) {
                    [vc.selectedGuids addObject:data.equip_id];
                }
            }
            vc.machineSelectedBlock = ^(NSArray *equips) {
                [self updateMainEquip:equips inSection:indexPath.section];
            };
            [self.navigationController pushViewController:vc animated:YES];
        } else {
        }
    }
}

- (void)updateMainEquip:(NSArray *)equips inSection:(NSInteger)section{
    MainEquipSystemData *item = [kProject.main_equip_systems objectAtIndex:section];
    NSMutableArray *ids = [NSMutableArray new];
    if (equips != nil && equips.count > 0) {
        for (NSDictionary *sel in equips) {
            [ids addObject:[sel objectForKey:@"Id"]];
        }
    }
    NSMutableDictionary *param = [NSMutableDictionary new];
    if (ids.count > 0) {
        [param setObject:[ids componentsJoinedByString:@","] forKey:@"ids"];
    }
    [param setObject:item.project_main_equip_system_id forKey:@"project_main_equip_system_id"];
    [MBProgressHUD showMessage:@"数据提交中..." toView:self.view];
    [HttpClient getWithUrl:HTTP_GET_PROJECT_MAIN_EQUIP_ADD token:[AccountManager manager].token paramDic:param resultClass:[MainEquipSystemData class] success:^(MainEquipSystemData *obj) {
        //oc对象归档
        [MBProgressHUD hideHUDForView:self.view];
        [kProject.main_equip_systems replaceObjectAtIndex:section withObject:obj];
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationAutomatic];
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view];
        [MBProgressHUD showError:@"数据提交失败！"];
    }];
}



- (KSelectStatus)checkRoomStatus:(RoomData *)room inSWJ:(MainEquipSystemData *)data {
    if ([data.project_main_equip_system_id isEqualToString:room.project_main_equip_system_id] ) {
        return KKSELECTED;
    } else if (![Tools isEmpty:room.project_main_equip_system_id]) {
        return KKSELECTED_OTHER;
    }
    return KKSELECTED_NOT;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        //系统cell
        SwjInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"swj"];
        return [cell getHeight];
    } else {
        NSInteger roomIndex = 0, equipIndex = 0;
        roomIndex = indexPath.row - 1;
        equipIndex = indexPath.row - self.tempRooms.count - 2;
        if (roomIndex < self.tempRooms.count) {
            //房间
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"room"];
            return cell.height;
        } else if (roomIndex == self.tempRooms.count) {
            //选择主机
            EditorTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"main_equip"];
            return [cell getHeight];
        } else {
            MachineSelectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"main_equip_cell"];
            return [cell getHeigh];
        }
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreen_Width, DEFAULT_SECTION_HEIGHT)];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return DEFAULT_SECTION_HEIGHT;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreen_Width, CGFLOAT_MIN)];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}

#pragma mark - 滑动按钮代理协议
/**
 *  右侧滑动按钮点击事件
 *
 *  @param cell  单元cell
 *  @param index 下标
 */
-(void)swipeableTableViewCell:(SWTableViewCell *)cell didTriggerRightUtilityButtonWithIndex:(NSInteger)index{
    if (cell.tag >= 1000) {
        //删除系统
        [self deleteMainEquipSystem:cell.tag - 1000];
    } else {
        //删除设备
        NSInteger section = cell.tag/100;
        NSInteger index = cell.tag%100;
        [self deleteMainEquip:section andIndex:index];
    }
    [cell hideUtilityButtonsAnimated:YES];
}

- (void)deleteMainEquipSystem:(NSInteger)section{
    MainEquipSystemData *system = [kProject.main_equip_systems objectAtIndex:section];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:[system project_main_equip_system_id] forKey:@"project_main_equip_system_id"];
    [params setValue:kProject.project_id forKey:@"project_id"];
    [MBProgressHUD showMessage:@"数据提交中..." toView:self.view];
    [HttpClient getWithUrl:HTTP_GET_PROJECT_MAIN_EQUIP_SYSTEM_DELETE token:[AccountManager manager].token paramDic:params resultClass:[MainEquipSystemData class] success:^(MainEquipSystemData *obj) {
        [MBProgressHUD hideHUDForView:self.view];
        [kProject.main_equip_systems removeObjectAtIndex:section];
        kProject.rooms = [obj.rooms mutableCopy];
        self.tempRooms = [obj.rooms mutableCopy];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view];
        [Tools showMessageCenter:@"删除失败！"];
    }];
}

- (void)deleteMainEquip:(NSInteger)section andIndex:(NSInteger)index {
    MainEquipSystemData *system = [kProject.main_equip_systems objectAtIndex:section];
    MainEquipData *equip = [system.equips objectAtIndex:index];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:[system project_main_equip_system_id] forKey:@"project_main_equip_system_id"];
    [params setValue:[equip equip_id] forKey:@"equip_id"];
    
    [MBProgressHUD showMessage:@"数据提交中..." toView:self.view];
    [HttpClient getWithUrl:HTTP_GET_PROJECT_MAIN_EQUIP_DELETE token:[AccountManager manager].token paramDic:params resultClass:[MainEquipSystemData class] success:^(MainEquipSystemData *obj) {
        [MBProgressHUD hideHUDForView:self.view];
        [kProject.main_equip_systems replaceObjectAtIndex:section withObject:obj];
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationAutomatic];
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view];
        [Tools showMessageCenter:@"删除失败！"];
    }];
}

- (void)updateMainEquipCount:(NSInteger)count andSection:(NSInteger)section andIndex:(NSInteger)index {
    MainEquipSystemData *system = [kProject.main_equip_systems objectAtIndex:section];
    MainEquipData *equip = [system.equips objectAtIndex:index];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:[system project_main_equip_system_id] forKey:@"project_main_equip_system_id"];
    [params setValue:[equip equip_id] forKey:@"equip_id"];
    [params setValue:@(count) forKey:@"count"];
    
    [MBProgressHUD showMessage:@"数据提交中..." toView:self.view];
    [HttpClient getWithUrl:HTTP_GET_PROJECT_MAIN_EQUIP_COUNT token:[AccountManager manager].token paramDic:params resultClass:[MainEquipSystemData class] success:^(MainEquipSystemData *obj) {
        [MBProgressHUD hideHUDForView:self.view];
        [kProject.main_equip_systems replaceObjectAtIndex:section withObject:obj];
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationAutomatic];
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view];
        [Tools showMessageCenter:@"修改失败！"];
    }];
}

- (void)modifyMainEquipSystemRoom:(NSString*)project_room_id andSection:(NSInteger)section {
    MainEquipSystemData *system = [kProject.main_equip_systems objectAtIndex:section];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:[system project_main_equip_system_id] forKey:@"project_main_equip_system_id"];
    [params setValue:kProject.project_id forKey:@"project_id"];
    [params setValue:project_room_id forKey:@"project_room_id"];
    
    [MBProgressHUD showMessage:@"数据提交中..." toView:self.view];
    [HttpClient getWithUrl:HTTP_GET_PROJECT_MAIN_EQUIP_ROOM_MODIFY token:[AccountManager manager].token paramDic:params resultClass:[ProjectData class] success:^(ProjectData *obj) {
        [MBProgressHUD hideHUDForView:self.view];
        kProject.main_equip_systems = [obj.main_equip_systems mutableCopy];
        kProject.rooms = [obj.rooms mutableCopy];
        self.tempRooms = [obj.rooms mutableCopy];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view];
        [Tools showMessageCenter:@"修改失败！"];
    }];
}



/**
 *  滑动的时候把其他的隐藏掉
 *
 *  @param cell cell
 *
 *  @return 隐藏其他的cell
 */
-(BOOL)swipeableTableViewCellShouldHideUtilityButtonsOnSwipe:(SWTableViewCell *)cell{
    return YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)editCoeff:(NSInteger)section couldCancel:(BOOL) couldCancel{
    CustomIOSAlertView *alert = [[CustomIOSAlertView alloc] init];
    [alert setUseMotionEffects:YES];
    
    UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width/2 + 40, 120)];
    
    //标题栏
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, subView.width, 50)];
    [titleView setUserInteractionEnabled:YES];
    
    titleView.backgroundColor = CM_DARKGREEN_COLOR;
    [subView addSubview:titleView];
    //左上角、右上角圆角为7
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:titleView.bounds byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(7, 7)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = titleView.bounds;
    maskLayer.path = maskPath.CGPath;
    titleView.layer.mask = maskLayer;
    
    //添加文字
    UILabel *titleLabel = [UILabel labelSingleLineFontSize:14 content:@"设置同时使用系数"];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.centerY = titleView.centerY;
    titleLabel.centerX = titleView.centerX;
    [titleView addSubview:titleLabel];
    
    UITextField *priceTextField = [[UITextField alloc]initWithFrame:CGRectMake(20, 70, subView.width - 40, 30)];
    priceTextField.borderStyle = UITextBorderStyleRoundedRect;
    priceTextField.textColor = CM_TABLE_DETAIL_COLOR;
    priceTextField.font = [UIFont systemFontOfSize:kListTitleFontSize];
    priceTextField.keyboardType=UIKeyboardTypeDecimalPad;//设置键盘类型为数字的
    priceTextField.returnKeyType=UIReturnKeyDefault;//返回键的类型
    [priceTextField addTarget:self action:@selector(coeffTextFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
    priceTextField.tag = section;
    MainEquipSystemData *system = [kProject.main_equip_systems objectAtIndex:section];
    if (couldCancel) {
        priceTextField.text = [NSString stringWithFormat:@"%0.2f", system.coeff];
        self->temp_coeff = system.coeff;
    }
    [subView addSubview:priceTextField];
    
    [alert setContainerView:subView];
    if (couldCancel) {
        [alert setButtonTitles:@[@"确定", @"取消"]];
    } else {
        [alert setButtonTitles:@[@"确定"]];
    }
    alert.closeOnTouchUpOutside = NO;
    @weakObj(alert)
    [alert setOnButtonTouchUpInside:^(CustomIOSAlertView *alertView, int buttonIndex) {
        switch (buttonIndex) {
            case 0:
                if (self->temp_coeff < 0.1 || self->temp_coeff > 1) {
                    [Tools showMessageCenter:@"系数设置错误"];
                    return;
                }
                [Weakalert close];
//                if (system.rooms.count == 0) {
//                    system.coeff = self->temp_coeff;
//                    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationAutomatic];
//                    return;
//                }
                [self modifyCoeff:section];
                break;
            case 1:
                [Weakalert close];
                break;
            default:
                break;
        }
    }];
    
    [alert show];
}

- (void)modifyCoeff:(NSInteger)section {
    MainEquipSystemData *equip = [kProject.main_equip_systems objectAtIndex:section];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:[NSNumber numberWithDouble:temp_coeff] forKey:@"coeff"];
    [params setValue:[equip project_main_equip_system_id] forKey:@"project_main_equip_system_id"];
    [MBProgressHUD showMessage:@"数据提交中..." toView:self.view];
    [HttpClient getWithUrl:HTTP_GET_PROJECT_MAIN_EQUIP_COEFF token:[AccountManager manager].token paramDic:params resultClass:[MainEquipSystemData class] success:^(MainEquipSystemData *obj) {
        [MBProgressHUD hideHUDForView:self.view];
        [kProject.main_equip_systems replaceObjectAtIndex:section withObject:obj];
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationAutomatic];
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view];
        [Tools showMessageCenter:@"修改失败！"];
    }];
}

/**
 修改同时使用系数

 @param obj <#obj description#>
 */
- (void)coeffTextFieldEditingChanged:(UITextField *)obj {
    temp_coeff = [obj.text doubleValue];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
